package com.zide.system.controller.monitor;

import com.zide.common.annotation.Log;
import com.zide.common.constant.CacheConstants;
import com.zide.common.core.domain.CommonResult;
import com.zide.common.core.redis.RedisCache;
import com.zide.common.enums.BusinessType;
import com.zide.common.utils.StringUtils;
import com.zide.system.domain.SysUserOnline;
import com.zide.system.service.ISysUserOnlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static com.zide.common.core.domain.CommonResult.success;

/**
 * 在线用户监控
 *
 * @author zide
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController {
    @Autowired
    private ISysUserOnlineService userOnlineService;

    @Autowired
    private RedisCache redisCache;

    @GetMapping("/list")
    public CommonResult list(String ipaddr, String username) {
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return success(userOnlineList);
    }

    /**
     * 强退用户
     */
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public CommonResult forceLogout(@PathVariable String tokenId) {
        redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
        return success();
    }
}
